/*
' Copyright (c) 2014  Diadrasis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using System.Web.Hosting;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;

//folders
using DotNetNuke.Services.FileSystem;
using System.Collections.Generic;
using System.Collections;
using DotNetNuke.Common.Utilities;
using System.IO;
using System.Web.UI.WebControls;

namespace net.diadrasis.dnn.jsImageGallery
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Settings class manages Module Settings
    /// 
    /// Typically your settings control would be used to manage settings for your module.
    /// There are two types of settings, ModuleSettings, and TabModuleSettings.
    /// 
    /// ModuleSettings apply to all "copies" of a module on a site, no matter which page the module is on. 
    /// 
    /// TabModuleSettings apply only to the current module on the current page, if you copy that module to
    /// another page the settings are not transferred.
    /// 
    /// If you happen to save both TabModuleSettings and ModuleSettings, TabModuleSettings overrides ModuleSettings.
    /// 
    /// Below we have some examples of how to access these settings but you will need to uncomment to use.
    /// 
    /// Because the control inherits from jsImageGallerySettingsBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Settings : jsImageGallerySettingsBase
    {

        #region Base Method Implementations

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void LoadSettings()
        {
            try
            {
                if (Page.IsPostBack == false)
                {
                    //Check for existing settings and use those on this page
                    //Settings["SettingName"]
                   string modPath=Server.MapPath("~/")+"DesktopModules\\jsImageGallery\\js\\";
                   string f1=modPath+"dataFadeImagesHome.js";
                   string f2 = modPath + "fadeImages.js";

                  if (System.IO.File.Exists(f1)){                      
                       ddlJSFile.Items.Add(new ListItem("home images", "dataFadeImagesHome.js"));                      
                   }
                   if (System.IO.File.Exists(f2)){
                       ddlJSFile.Items.Add(new ListItem("fade images", "fadeImages.js"));                      
                   }
                    
                   if (Settings.Contains("jsFile1")){
                       ddlJSFile.SelectedValue = Settings["jsFile1"].ToString();
                   }else{
                       ddlJSFile.SelectedValue = "fadeImages.js";
                   }

                   ddlPosition.Items.Add(new ListItem("in current module"));
                   ddlPosition.Items.Add(new ListItem("in media module"));

                   if (Settings.Contains("modulePosition")){
                       ddlPosition.SelectedValue = Settings["modulePosition"].ToString();
                   }else{
                       ddlPosition.SelectedValue="in current module";
                   }

                   if (Settings.Contains("showTime")){
                       txtTime.Text = Settings["showTime"].ToString();
                   } else {
                       txtTime.Text = "4";
                   }

                   if (Settings.Contains("puzzleItemsHoriz"))
                   {
                       txtPuzzleHorizontal.Text = Settings["puzzleItemsHoriz"].ToString();
                   }
                   else
                   {
                       txtPuzzleHorizontal.Text = "1";
                   }

                   if (Settings.Contains("puzzleItemsVert"))
                   {
                       txtPuzzleVertical.Text = Settings["puzzleItemsVert"].ToString();
                   }
                   else
                   {
                       txtPuzzleVertical.Text = "1";
                   }                  
                   

                   if (Settings.Contains("puzzleEffect"))
                   {
                       cbPuzzleEffect.Checked = Convert.ToBoolean(Settings["puzzleEffect"].ToString());
                   }
                   else
                   {
                       txtPuzzleVertical.Text = "1";
                   }

                   if (Settings.Contains("imageRatio")) {
                       txtImageRatio.Text= Settings["imageRatio"].ToString();
                   } else {
                       txtImageRatio.Text = "2.5";
                   }

                   if (Settings.Contains("initImage"))
                   {
                       urlcInitImage.Url = Settings["initImage"].ToString();
                   } 
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpdateSettings saves the modified settings to the Database
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void UpdateSettings()
        {
            try
            {
                ModuleController modules = new ModuleController();
                //tab module settings
                modules.UpdateTabModuleSetting(TabModuleId, "jsFile1", ddlJSFile.SelectedValue);
                modules.UpdateTabModuleSetting(TabModuleId, "modulePosition", ddlPosition.SelectedValue);
                modules.UpdateTabModuleSetting(TabModuleId, "showTime", txtTime.Text);
                modules.UpdateTabModuleSetting(TabModuleId, "puzzleItemsHoriz", txtPuzzleHorizontal.Text);
                modules.UpdateTabModuleSetting(TabModuleId, "puzzleItemsVert", txtPuzzleHorizontal.Text);
                modules.UpdateTabModuleSetting(TabModuleId, "puzzleEffect", cbPuzzleEffect.Checked.ToString());
                modules.UpdateTabModuleSetting(TabModuleId, "imageRatio", txtImageRatio.Text);
                modules.UpdateTabModuleSetting(TabModuleId, "initImage", urlcInitImage.Url);
              
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }

}

